package com.copa.l94;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-05-19 09:11
 * @function 递归法
 */
public class Test1 {
    private List<Integer> result = new ArrayList<>();

    public List<Integer> inorderTraversal(TreeNode root) {
        // 注意 中序遍历的顺序是：左 根 右
        if (root != null) {
            // 先遍历左
            inorderTraversal(root.left);
            // 取值
            result.add(root.val);
            // 再遍历右
            inorderTraversal(root.right);
        }
        return result;
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode();
        treeNode.val = 1;
        treeNode.right = new TreeNode(2);
        treeNode.right.left = new TreeNode(3);
        List<Integer> result = new Test1().inorderTraversal(treeNode);
        System.out.println(Arrays.toString(result.toArray()));
    }
}
